
class Code extends Code3a{
	private static boolean error = false;
	 public Code (){
		 super ();
	 }
	 
		 public Code(Inst3a i){
		 super(i);
	 }


	 public static Code genBinOp(int op, Expratt exp1, Expratt exp2, Operand3a temp){
		  Code cod = exp1.code;
		  cod.append(exp2.code);
		  cod.append(new Inst3a(Inst3a.TAC_VAR, temp, null, null)); 
		  Inst3a i = new Inst3a(op, temp, exp1.place, exp2.place);
		  cod.append(new Code(i));
		  return cod;
	 }

	public static Code genIf (Expratt exp, Code cvrai ,Code cfaux)
	{	
		Code cod;
		Inst3a i;
		Tokatt sifaux;
		Tokatt fin;

		sifaux = SymbDistrib.newLabel();
		fin = SymbDistrib.newLabel();
		cod = exp.code;		

		i = new Inst3a(Inst3a.TAC_IFZ, exp.place ,sifaux, null);
		cod.append(new Code(i));
		cod.append(cvrai);
		i = new Inst3a(Inst3a.TAC_GOTO, fin, null, null);
		cod.append(new Code(i));
		i = new Inst3a(Inst3a.TAC_LABEL, sifaux, null, null);
		cod.append(new Code(i));
		cod.append(cfaux);
		i = new Inst3a(Inst3a.TAC_LABEL, fin, null, null);
		cod.append(new Code(i));
		return cod;
	}

	public static Code genWhile (Expratt exp, Code c)
	{
		Code cod;
		Inst3a i;
		Tokatt debut;
		Tokatt fin;

		debut = SymbDistrib.newLabel();
		fin = SymbDistrib.newLabel();
		cod = new Code();
		i = new Inst3a(Inst3a.TAC_LABEL, debut, null, null);
		cod.append(new Code(i));
		cod.append(exp.code); 
		i = new Inst3a(Inst3a.TAC_IFZ, exp.place,fin, null);
		cod.append(new Code(i));
		cod.append(c);
		i = new Inst3a(Inst3a.TAC_GOTO, debut,null,null);
		cod.append(new Code(i));
		i = new Inst3a(Inst3a.TAC_LABEL, fin,null,null);
		cod.append(new Code(i));
		return cod;
	}

	public static Code genPrintText (String t)
	{

		Tokatt L4;
		Code cod;
		Inst3a i;
		Tokatt t2;
		
		t2 = new Tokatt(t);
		L4 = new Tokatt("L4",null);
		cod = new Code();
		i = new Inst3a(Inst3a.TAC_ARG, t2.label, null, null);
		cod.append(new Code(i));
		i = new Inst3a(Inst3a.TAC_CALL, null, L4, null);
		cod.append(new Code(i));
		cod.appendData(t2);
		return cod;
	}

	public static Code genPrintExpr (Expratt exp)
	{
		Code cod;
		Inst3a i;
		Tokatt L2;
		Tokatt temp;
		
		cod = exp.code;
		L2 = new Tokatt("L2",null);
		temp = SymbDistrib.newTemp();
		i = new Inst3a(Inst3a.TAC_VAR, temp, null, null);
		cod.append(new Code(i));
		i = new Inst3a(Inst3a.TAC_COPY,temp,exp.place,null);
		cod.append(new Code(i));
		i = new Inst3a(Inst3a.TAC_ARG, temp, null, null);
		cod.append(new Code(i));
		i = new Inst3a(Inst3a.TAC_CALL, null, L2, null);
		cod.append(new Code(i));
		return cod;
	}

	public static Code genPrintList (Code p1, Code p2)
	{
		Code cod;

		cod = p1;
		cod.append(p2);
		return cod;
	}

	public static Code genBlock (Code d, Code s)
	{
		Code cod;
		
		cod = d;
		cod.append(s);
		return cod;
	}

	public static Code genArgList (Expratt e)
	{
		Code cod;
		Inst3a i;
		
		cod = e.code;
		return cod;
	}
		
} 
